package other;

import java.io.Serializable;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import android.content.Context;
import android.database.Cursor;

public class Clog implements Serializable {
	private static final long serialVersionUID = 6498706456451135225L;
	private String num; 
	private String type;
	private String name;
	private String duration;
	private String date;
	//Context context;
	public Clog(String num1,String type1,String name1,String duration1,String date1){
		num=num1;type=type1;
		name=name1;duration=duration1;date=date1;}
	public Clog(){};

	public List<Clog> getlog(Context context){
		List<Clog> logtable= new ArrayList<Clog>();

		
		String[] strFields = {
		        android.provider.CallLog.Calls.NUMBER, 
		        android.provider.CallLog.Calls.TYPE,
		        android.provider.CallLog.Calls.CACHED_NAME,
		        android.provider.CallLog.Calls.DURATION,
		        android.provider.CallLog.Calls.DATE
		        };
		String strOrder = android.provider.CallLog.Calls.DATE + " DESC"; 
		 
		Cursor mCallCursor = context.getContentResolver().query(
		        android.provider.CallLog.Calls.CONTENT_URI,
		        strFields,
		        null,
		        null,
		        strOrder
		        );
		
		if(mCallCursor.moveToFirst()){
			 
			  // loop through cursor 
			  do{

				  String name=mCallCursor.getString(2);

				  String num=mCallCursor.getString(0);

				  String calld=mCallCursor.getString(3);
				  long l = Long.valueOf(calld);
				  int secs=(int)(l)% 60;
				  int mins=(int)(l/(60)) % 60;
				  int hrs=(int)(l/(60*60));
				  String duration = (String.valueOf(hrs)+":"+String.valueOf(mins)+":"+String.valueOf(secs));

		            String callType = mCallCursor.getString(1);
		            String dir = "Not Found";
		            int dircode = Integer.parseInt(callType);
		            switch (dircode) {
		            case 2:
		                dir = "OUTGOING";
		                break;

		            case 1:
		                dir = "INCOMING";
		                break;

		            case 3:
		                dir = "MISSED";
		                break;
		            }
		            String type=dir;
				  
		            String callDate = mCallCursor.getString(4);
		            Date callDayTime = new Date(Long.valueOf(callDate));
					SimpleDateFormat formatDateJour = new SimpleDateFormat("dd/MM/yyyy kk:mm:ss");
					String date = formatDateJour.format(callDayTime);
					Clog call=new Clog(num,type, name,duration,date);
					logtable.add(call);
					
			  } while (mCallCursor.moveToNext());
			 
			}
		return logtable;
	}
	public String getnum(){return num;}
	public String gettype(){return type;}
	public String getname(){return name;}
	public String getduration(){return duration;}
	public String getdate(){return date;}

}
